package com.util;

import java.util.Properties;
import java.util.Random;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public final class MailUntil {
    public static Session createSession() {

        // 账号信息
        String username = "1979966109@qq.com";// 邮箱发送账号
        String password = "owvunmisisilehhe";// 邮箱授权码

        // 创建一个配置文件，并保存
        Properties props = new Properties();

        // SMTP服务器连接信息，修改为QQ邮箱对应的SSL配置
        props.put("mail.smtp.host", "smtp.qq.com");// SMTP主机名
        props.put("mail.smtp.port", "465");// 主机端口号，使用SSL时常用465端口
        props.put("mail.smtp.auth", "true");// 是否需要用户认证
        props.put("mail.smtp.ssl.enable", "true");// 启用SSL加密

        // 以下是设置信任所有证书的逻辑（在测试环境下可以这样简单处理，但生产环境可能需要更严谨配置合适的证书信任策略）
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            }}, new java.security.SecureRandom());
            props.put("mail.smtp.ssl.socketFactory", sslContext.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        // 控制台打印调试信息
        session.setDebug(false);
        return session;
    }


    public static int sendemail(String qq_target) throws MessagingException {
        // 创建Session会话
        Session session = MailUntil.createSession();
        String qq_email = qq_target ;

        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        message.setSubject("客户管理系统新用户注册验证码");

        Random random = new Random();
        int verificationCode = 0;
        for (int i = 0; i < 4; i++) {
            // 生成0 - 9的随机数字
            int digit = random.nextInt(10);
            verificationCode = verificationCode * 10 + digit;
        }
        message.setText("您的验证码是：" + verificationCode);
        message.setFrom(new InternetAddress("1979966109@qq.com"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(qq_email));

        // 发送
        Transport.send(message);
        return  verificationCode;
    }

   /* public static void main(String[] args) throws MessagingException {
        Session session = JavaMailUntil.createSession();


        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        message.setSubject("客户管理系统新用户注册验证码");

        Random random = new Random();
        int verificationCode = 0;
        for (int i = 0; i < 4; i++) {
            // 生成0 - 9的随机数字
            int digit = random.nextInt(10);
            verificationCode = verificationCode * 10 + digit;
        }

        message.setText("您的验证码是：" + verificationCode);
        message.setFrom(new InternetAddress("1979966109@qq.com"));
        String qq_email = "3011335317@qq.com" ;
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(qq_email));

        // 发送
        Transport.send(message);
    }*/








}
